package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.member.dao.MemberReceiveAddressDao;
import com.atguigu.gulimall.member.entity.MemberReceiveAddressEntity;
import com.atguigu.gulimall.member.service.MemberReceiveAddressService;
import com.atguigu.gulimall.member.vo.FareVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取用户地址信息
     * @param
     * @return
     */
    @Override
    public List<MemberReceiveAddressEntity> getAddress(Long memberId) {

        List<MemberReceiveAddressEntity> member_id = this.baseMapper.selectList(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId));
        return member_id;


    }


    /**
     * 根据id获取收获地址和运费信息
     * @param id
     * @return
     */
    @Override
    public FareVo getAddressId(Long id) {
        FareVo vo = new FareVo();
        MemberReceiveAddressEntity memberReceiveAddressEntity = this.baseMapper.selectById(id);
        vo.setMemberReceiveAddressEntity(memberReceiveAddressEntity);
        String phone = memberReceiveAddressEntity.getPhone();
        if (phone!=null){
            String substring = phone.substring(phone.length()-1,phone.length());
            vo.setFare(new BigDecimal(substring));
        }
        return vo;
    }
}